"""
作物，病害service
最后修改：3.29
修改人：陈泯全
"""
from flask import current_app
from utils.dataBase import db_session
from models.PrevenCureWikiSubSysModels import Disease,Speices
class prevenCureWikiService(object):

    def __init__(self):
        pass
    """
    查询
    """
    def search(self,name):
        if name ==None and name =='':
            current_app.logger.error('查询参数错误')
            return []
        list = []
        d = db_session.query(Disease).filter(Disease.dname.like('%{}%'.format(name))).all()
        s = db_session.query(Speices).filter(Speices.sname.like('%{}%'.format(name))).all()
        current_app.logger.info('查询到病害条数：{}，查询到物种:{}'.format(len(d),len(s)))
        #先查物种
        for item in d:
            dict = {}
            dict['name'] = item.dname
            dict['label'] = 2
            dict['info'] = item.dinfo
            list.append(dict)
        for item in s:
            dict = {}
            dict['name'] = item.sname
            dict['label'] = 1
            dict['info'] = item.sinfo
            list.append(dict)
        return list

    """
    疾病查询
    """
    def disease(self,dname):
        if dname ==None and dname =='':
            current_app.logger.error('查询参数错误')
            return []
        d = db_session.query(Disease).filter(Disease.dname.like('%{}%'.format(dname))).all()
        current_app.logger.info('查询到条数：{}'.format(len(d)))
        list = []
        for item in d:
            dict = {}
            dict['dinfo'] = item.dinfo
            dict['dheal'] = item.dheal
            dict['dprevent'] = item.dprevent
            list.append(dict)
        return list

    """
    物种查询
    """
    def speices(self,sname):
        if sname ==None and sname =='':
            current_app.logger.error('查询参数错误')
            return []
        s = db_session.query(Speices).filter(Speices.sname.like('%{}%'.format(sname))).all()
        current_app.logger.info('查询到条数：{}'.format(len(s)))
        list = []
        for item in s:
            dict = {}
            dict['sname'] = item.sname
            dict['sinfo'] = item.sinfo
            dict['ssug'] = item.ssug
            list.append(dict)
        return list

    """
    物种列表
    """
    def speiceslist(self):
        list = db_session.query(Speices.sname).all()
        return list

    """
     病害列表
     """
    def diseaselist(self):
        list = db_session.query(Disease.dname).all()
        return list

